{ "cells": [ { "cell_type": "markdown", "id": "2e5e7b08", "metadata": {}, "source": [ "# Variables — Exercise Cards (Class room version)\n", "## Try me\n", "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/ffraile/computer_science_tutorials/blob/main/source/Introduction/exercises/variables%20cards.ipynb)[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ffraile/computer_science_tutorials/main?labpath=source%2FIntroduction%2Fexercises%2Fvariables%20cards.ipynb)\n", "\n", "## How to use\n", "- Each card below mirrors the A4 activity prompt. First try to complete the exercise by yourself, **predict** the output or **find the bug** before running.\n", "- Then run the code cell to verify, or run the **Fix** cell (when provided) to see a correct version.\n", "- Keep explanations short and schematic (what/why).\n", "### Rendering Gemini into an interactive AI tutor\n", "- If you need help, hints, or extra exercises from AI, use the following prompt to convert it into an AI-tutor\n", "\n", "```markdown\n", "You are a **coding tutor** for Python in Jupyter/Colab. Follow the **course motto** “do not give up learning.”\n", "\n", "### Role & Goals\n", "- Use **Socratic guidance** and **test-first thinking** to help me solve problems myself.\n", "- Help me read errors, reason about state, and make small, safe iterations.\n", "\n", "### Strict Rules\n", "1) **Do not** provide full working solutions or paste complete functions/programs.\n", " - You may show **tiny illustrative fragments (≤3 lines)** or **pseudo-code with TODOs**, but not a drop-in answer.\n", "2) Prefer **questions over answers**; offer **one small next step** at a time.\n", "3) When debugging, explain **what the traceback says**, give **2–3 hypotheses**, and propose the **smallest diff** in *plain English* first.\n", "4) Encourage **TDD**: ask me to write/assert a test, predict, run, and report outputs.\n", "5) Keep responses concise (≈120–150 words) unless I ask for a deeper explanation or code review.\n", "6) Ask me to **run code and share results**; adapt based on the output.\n", "7) If I request the full solution, remind me of the rules and offer a **higher-tier hint** instead.\n", "8) When I finalize an exercise, reinforce learning lessons and suggest additional exercises\n", "\n", "### Interaction Loop (use this structure)\n", "- **Restate goal:** what I’m trying to accomplish in one line.\n", "- **Diagnose:** key assumption to check or error to interpret.\n", "- **Hint (tiered):**\n", " - Tier 1: Conceptual nudge (no code).\n", " - Tier 2: Directed hint (identify line/construct to change).\n", " - Tier 3: Pseudo-code with TODOs or a **1–3 line** pattern (still not a full solution).\n", "- **Next action:** one concrete step for me to try now.\n", "- **Ask back:** what to run/paste (output, test result, or traceback).\n", "\n", "### When reviewing my code\n", "- Comment on **correctness, clarity, naming, and complexity (big-O)**.\n", "- Suggest **tests** I’m missing (boundaries, empty cases, error paths).\n", "\n", "### Safety & Ethics\n", "- No secrets or private data in prompts.\n", "- avoid library functions/APIs unless I ask.\n", "\n", "Stay in tutor mode for the whole session.\n", "```" ] }, { "cell_type": "markdown", "id": "3f36bfb0", "metadata": {}, "source": "## Numeric Operations" }, { "cell_type": "markdown", "id": "a9f84942", "metadata": {}, "source": [ "### Card N-1 — Human Calculator\n", "```python\n", "n = 9//2\n", "n = n**2\n", "# What is the value of n after the script?\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "61d4ae83", "metadata": {}, "outputs": [], "source": [ "\n", "# Card N-1 — check\n", "n = 9//2\n", "n = n**2\n", "print(n)" ] }, { "cell_type": "markdown", "id": "18612a8e", "metadata": {}, "source": [ "### Card N-2 — Human Calculator\n", "```python\n", "# y = x % 2\n", "# What is y when x=9? and when x=8?\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "cd179409", "metadata": {}, "outputs": [], "source": [ "\n", "# Card N-2 — check with two samples (no input)\n", "def parity(x: int) -> int:\n", " return x % 2\n", "\n", "print(\"x=9 -> y=\", parity(9))\n", "print(\"x=8 -> y=\", parity(8))" ] }, { "cell_type": "markdown", "id": "25d6c8db", "metadata": {}, "source": [ "### Card N-3 — Code Detective (Find the bug)\n", "Buggy Code:\n", "```python\n", "x = ((1 + 2) * 3 / (4 - 0.5)\n", "```\n", "**Fix it** so it runs witout errors." ] }, { "cell_type": "code", "execution_count": null, "id": "4627e92c", "metadata": {}, "outputs": [], "source": [ "\n", "# Card N-3 — fixed\n", "x = ((1 + 2) * 3 / (4 - 0.5)\n", "print(x)" ] }, { "cell_type": "markdown", "id": "7a143172", "metadata": {}, "source": [ "### Card N-4 — Human Calculator\n", "```python\n", "n = 9//2 - 2 ** 2\n", "# What is the result?\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "f742757d", "metadata": {}, "outputs": [], "source": [ "\n", "# Card N-4 — check\n", "n = 9//2 - 2 ** 2\n", "print(n)" ] }, { "cell_type": "markdown", "id": "18d8ca88", "metadata": {}, "source": [ "### Card N-5 — Human Calculator\n", "```python\n", "x = 4+5j\n", "y = 3+3j\n", "print(x.imag + y.imag)\n", "```\n", "What is printed?" ] }, { "cell_type": "code", "execution_count": null, "id": "d27cc6d4", "metadata": {}, "outputs": [], "source": [ "\n", "# Card N-5 — check\n", "x = 4+5j\n", "y = 3+3j\n", "print(x.imag + y.imag)" ] }, { "cell_type": "markdown", "id": "af5aacfc", "metadata": {}, "source": "## Strings" }, { "cell_type": "markdown", "id": "c4199a74", "metadata": {}, "source": [ "### Card S-1 — Code Detective (Find the bug)\n", "Buggy:\n", "```python\n", "my_message = Hello World\n", "```\n", "**Fix** the string assignment." ] }, { "cell_type": "code", "execution_count": null, "id": "d4013758", "metadata": {}, "outputs": [], "source": [ "\n", "# Card S-1 — fixed\n", "my_message = Hello World\n", "print(my_message) # expected: Hello World" ] }, { "cell_type": "markdown", "id": "2bc54062", "metadata": {}, "source": [ "### Card S-2 — Human Calculator\n", "```python\n", "x = \"2\"\n", "y = 3\n", "print(x * y)\n", "```\n", "# What is printed?" ] }, { "cell_type": "code", "execution_count": null, "id": "081179b3", "metadata": {}, "outputs": [], "source": [ "\n", "# Card S-2 — check\n", "x = \"2\"\n", "y = 3\n", "print(x * y)" ] }, { "cell_type": "markdown", "id": "166c7b9b", "metadata": {}, "source": [ "### Card S-3 — Code Detective (What does this bot do?)\n", "Original idea:\n", "```python\n", "a = input(\"What did you say\")\n", "print(a + \"- duh-duuh\")\n", "```\n", "**Question:** What does the bot do? *(We simulate without input.)*" ] }, { "cell_type": "code", "execution_count": null, "id": "86f31a27", "metadata": {}, "outputs": [], "source": [ "\n", "# Card S-3 — simulate without input\n", "a = \"I like Python\"\n", "print(a + \"- duh-duuh\") # expected: echoes input with suffix" ] }, { "cell_type": "markdown", "id": "5ffb521b", "metadata": {}, "source": [ "### Card S-4 — Human Calculator\n", "```python\n", "area = 9 # area covered\n", "total = 10 # total area\n", "print(f\"Area = {area}, covered: {area/total:.0%}\")\n", "```\n", "What is printed?" ] }, { "cell_type": "code", "execution_count": null, "id": "b72c77e6", "metadata": {}, "outputs": [], "source": [ "\n", "# Card S-4 — check\n", "area = 9\n", "total = 10\n", "print(f\"Area = {area}, covered: {area/total:.0%}\") # expected: Area = 9, covered: 90%" ] }, { "cell_type": "markdown", "id": "93b58de4", "metadata": {}, "source": [ "### Card S-5 — Human Calculator\n", "```python\n", "s = \"Hello\"\n", "print(s + \"!\")\n", "print(s.replace(\"l\",\"L\"))\n", "print(s)\n", "```\n", "What is printed (and why)?" ] }, { "cell_type": "code", "execution_count": null, "id": "cb8d3120", "metadata": {}, "outputs": [], "source": [ "\n", "# Card S-5 — check\n", "s = \"Hello\"\n", "print(s + \"!\") # Hello!\n", "print(s.replace(\"l\",\"L\")) # HeLLo\n", "print(s) # Hello (strings are immutable)" ] }, { "cell_type": "markdown", "id": "87231769", "metadata": {}, "source": "## Boolean Variables" }, { "cell_type": "markdown", "id": "2f9b01b6", "metadata": {}, "source": [ "### Card B-1 — Code Detective (Find the bug)\n", "```python\n", "x = 1 \n", "y = 2 \n", "a = x = y # a boolean variable?\n", "```\n", "**Question:** What's wrong with the comment, and how to get a boolean instead?" ] }, { "cell_type": "code", "execution_count": null, "id": "05803720", "metadata": {}, "outputs": [], "source": [ "\n", "# Card B-1 — explanation & fix\n", "x = 1\n", "y = 2\n", "a = x = y\n", "print(a, x, y) # 2 2 2\n", "\n" ] }, { "cell_type": "markdown", "id": "47a70176", "metadata": {}, "source": [ "### Card B-2 — Human Calculator\n", "Original idea (uses input):\n", "```python\n", "age = int(input(\"what is your age?\"))\n", "print(18 <= age < 65)\n", "print(bool(\"\"))\n", "print(bool(\"False\"))\n", "```\n", "**What gets printed?**\n", "*(Assume user enters the number 18.)*" ] }, { "cell_type": "code", "execution_count": null, "id": "eb1bb4aa", "metadata": {}, "outputs": [], "source": [ "\n", "# Card B-2 — simulate without input\n", "age = int(input(\"What is your age? \"))\n", "print(18 <= age < 65) # expected: True\n", "print(bool(\"\")) # expected: False (empty string is falsey)\n", "print(bool(\"False\")) # expected: True (non-empty strings are truthy)" ] }, { "cell_type": "markdown", "id": "d55e3a7c", "metadata": {}, "source": [ "### Card B-3 — Human Calculator\n", "```python\n", "def ping():\n", " print(\"ping\")\n", " return True\n", "print(False and ping())\n", "print(True or ping())\n", "```\n", "What is printed?" ] }, { "cell_type": "code", "execution_count": null, "id": "eb91453c", "metadata": {}, "outputs": [], "source": [ "\n", "# Card B-3 — check (short-circuit)\n", "def ping():\n", " print(\"ping\")\n", " return True\n", "\n", "print(False and ping()) # expected: False (ping not called)\n", "print(True or ping()) # expected: True (ping not called)" ] }, { "cell_type": "markdown", "id": "e72c5e81", "metadata": {}, "source": "## D) Extra Cards" }, { "cell_type": "markdown", "id": "3171bedd", "metadata": {}, "source": [ "### Extra N-E1 — Human Calculator\n", "```python\n", "print(2 ** 1 + 2)\n", "```\n", "# What is printed, and why?" ] }, { "cell_type": "code", "id": "fe022e35", "metadata": {}, "source": [ "\n", "# Extra N-E1 — check (operations priority)\n", "print(2 ** 1 + 2)" ], "outputs": [], "execution_count": null }, { "cell_type": "markdown", "id": "8b1c95f7", "metadata": {}, "source": [ "### Extra N-E2 — Code Wizard\n", "Fix the code below to parse the string `s =\"3.14\"` into a number." ] }, { "cell_type": "code", "execution_count": null, "id": "1e3de987", "metadata": {}, "outputs": [], "source": [ "\n", "# Extra N-E2 — demo\n", "s = \"3.14\"\n", "# print(int(s)) # ValueError if uncommented\n", "val = 3.4 # FIX: correct approach: parse as float\n", "print(val) # expected: 3.14" ] }, { "cell_type": "markdown", "id": "d3f90e58", "metadata": {}, "source": [ "### Extra S-E4 — Human Calculator\n", "```python\n", "print(f\"bin:{13:b} hex:{255:x}\")\n", "```\n", "# What is printed?" ] }, { "cell_type": "code", "execution_count": null, "id": "cc096055", "metadata": {}, "outputs": [], "source": [ "\n", "# Extra S-E4 — check\n", "print(f\"bin:{13:b} hex:{255:x}\") # expected: bin:1101 hex:ff" ] }, { "cell_type": "markdown", "id": "a84ec1d1", "metadata": {}, "source": [ "### Extra B-E5 — Human Calculator\n", "```python\n", "a = 1000; b = 1000\n", "print(a == b, a is b)\n", "```\n", "# What is printed, and why?" ] }, { "cell_type": "code", "execution_count": null, "id": "cdd82aa9", "metadata": {}, "outputs": [], "source": [ "\n", "# Extra B-E5 — check\n", "a = 1000; b = 1000\n", "print(a == b, a is b) # expected: True False (typically; equal value, different objects)" ] }, { "cell_type": "markdown", "id": "d0a4efb2", "metadata": {}, "source": [ "### Extra B-E6 — Human Calculator\n", "Predict the outputs:\n", "```python\n", "print(bool([]), bool([0]))\n", "print(bool({}), bool({\"x\":0}))\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "4ac25d61", "metadata": {}, "outputs": [], "source": [ "\n", "# Extra B-E6 — check\n", "print(bool([]), bool([0])) # expected: False True\n", "print(bool({}), bool({\"x\":0})) # expected: False True" ] }, { "cell_type": "markdown", "id": "2ab57ecc", "metadata": {}, "source": [ "### Extra B-E7 — Code Detective\n", "Avoid using `is` for string equality.\n", "```python\n", "code = \"OK\"\n", "# if code is \"OK\": # anti-pattern\n", "# print(\"ok\")\n", "```\n", "**Fix it**." ] }, { "cell_type": "code", "execution_count": null, "id": "13191fda", "metadata": {}, "outputs": [], "source": [ "\n", "# Extra B-E7 — fixed\n", "code = \"OK\"\n", "if code == \"OK\":\n", " print(\"ok\") # expected: ok" ] }, { "metadata": {}, "cell_type": "markdown", "source": [ "Extra B-8 — Human Calculator\n", "Predict the result\n", "```python\n", "print(True and True)\n", "print(not False or False)\n", "print(3 == 4)\n", "print(0.3 == 0.1 + 0.2)\n", "```" ], "id": "4838111e2b8eec60" }, { "metadata": {}, "cell_type": "code", "source": [ "print(True and True)\n", "print(not False or False)\n", "print(3 == 4)\n", "print(0.3 == 0.2 + 0.1)" ], "id": "f80e1ed3b2201c1b", "outputs": [], "execution_count": null } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.x" } }, "nbformat": 4, "nbformat_minor": 5 }